package android.slc.appbase.api.slc;

import android.content.Context;
import android.slc.appbase.api.slc.config.ApiConfig;
import android.slc.appbase.api.slc.converter.SlcConverterFactory;
import android.slc.appbase.api.slc.converter.pure.SlcConverterPureFactory;
import android.slc.appbase.api.slc.interceptor.TokenInterceptor;
import android.slc.appbase.config.ConstantsBase;
import android.slc.extras.component.attchment.utils.AttachmentConfig;
import android.slc.or.SlcNu;
import android.util.Log;

import com.liulishuo.okdownload.OkDownload;
import com.liulishuo.okdownload.core.connection.DownloadOkHttp3ConnectionFixBug;

import java.util.concurrent.TimeUnit;

import me.jessyan.retrofiturlmanager.RetrofitUrlManager;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;

public class SlcApi {
    public static void init(Context context) {
        HttpLoggingInterceptor httpLoggingInterceptor = null;
        if (!ConstantsBase.RELEASE) {
            httpLoggingInterceptor = new HttpLoggingInterceptor((String message) -> Log.d("OkHttp", message));
            httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
        }
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .readTimeout(12, TimeUnit.SECONDS)
                .writeTimeout(12, TimeUnit.SECONDS)
                .connectTimeout(12, TimeUnit.SECONDS)
                .addNetworkInterceptor(new TokenInterceptor());
        if (httpLoggingInterceptor != null) {
            builder.addNetworkInterceptor(httpLoggingInterceptor);
        }
        SlcNu.getInstance().setDefRetrofit(new Retrofit.Builder()
                .baseUrl(ApiConfig.getInstance().getServiceApiAddress())
                .client(builder.build())
                .addConverterFactory(SlcConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build());
        OkDownload.Builder okDownloadBuilder = new OkDownload.Builder(context).connectionFactory(new DownloadOkHttp3ConnectionFixBug.Factory());
        OkDownload.setSingletonInstance(okDownloadBuilder.build());

        AttachmentConfig.getInstance().setDownloadIpPort(ApiConfig.getInstance().getServiceApiAddress());
        AttachmentConfig.getInstance().setDownloadApiPart("common/download/resource?resource=");

        initPureApi();
        initSearchApi();
    }

    private static void initPureApi() {
        HttpLoggingInterceptor httpLoggingInterceptor = null;
        if (!ConstantsBase.RELEASE) {
            httpLoggingInterceptor = new HttpLoggingInterceptor((String message) -> Log.d("OkHttp", message));
            httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
        }
        //------------------------------------------------------------------------
        OkHttpClient.Builder autoSearch = new OkHttpClient.Builder()
                .readTimeout(1, TimeUnit.SECONDS)
                .writeTimeout(1, TimeUnit.SECONDS)
                .connectTimeout(1, TimeUnit.SECONDS)
                .addNetworkInterceptor(new TokenInterceptor());
        if (httpLoggingInterceptor != null) {
            autoSearch.addNetworkInterceptor(httpLoggingInterceptor);
        }
        //此工具是为了动态更换url，目前没有在其他地方用到该API
        RetrofitUrlManager.getInstance().with(autoSearch);

        SlcNu.getInstance().putRetrofit("pureApi", new Retrofit.Builder()
                .baseUrl(ApiConfig.getInstance().getServiceApiAddress())
                .client(autoSearch.build())
                .addConverterFactory(SlcConverterPureFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build());
        //--------------------------------------------------------
    }

    /**
     * 创建纯净的服务
     *
     * @param service
     * @param <T>
     * @return
     */
    public static <T> T createPureService(final Class<T> service) {
        return SlcNu.getInstance().create("pureApi", service);
    }

    private static void initSearchApi() {
        HttpLoggingInterceptor httpLoggingInterceptor = null;
        if (!ConstantsBase.RELEASE) {
            httpLoggingInterceptor = new HttpLoggingInterceptor((String message) -> Log.d("OkHttp", message));
            httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
        }
        //------------------------------------------------------------------------
        OkHttpClient.Builder autoSearch = new OkHttpClient.Builder()
                .readTimeout(1, TimeUnit.SECONDS)
                .writeTimeout(1, TimeUnit.SECONDS)
                .connectTimeout(1, TimeUnit.SECONDS)
                .addNetworkInterceptor(new TokenInterceptor());
        if (httpLoggingInterceptor != null) {
            autoSearch.addNetworkInterceptor(httpLoggingInterceptor);
        }
        //此工具是为了动态更换url，目前没有在其他地方用到该API
        RetrofitUrlManager.getInstance().with(autoSearch);

        SlcNu.getInstance().putRetrofit(ApiConfig.Key.KEY_AUTO_SEARCH, new Retrofit.Builder()
                .baseUrl(ApiConfig.getInstance().getServiceApiAddress())
                .client(autoSearch.build())
                .addConverterFactory(SlcConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build());
        //--------------------------------------------------------

    }

    /**
     * 创建搜索服务
     *
     * @param service
     * @param <T>
     * @return
     */
    public static <T> T createSearchService(final Class<T> service) {
        return SlcNu.getInstance().create(ApiConfig.Key.KEY_AUTO_SEARCH, service);
    }
}
